package com.ming.yygh.user.api;

import com.ming.yygh.common.result.Result;
import com.ming.yygh.common.utils.AuthContextHolder;
import com.ming.yygh.model.user.UserInfo;
import com.ming.yygh.user.service.UserInfoService;
import com.ming.yygh.vo.user.LoginVo;
import com.ming.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author Light
 * @date 2021/12/16 11:22
 */
@Api(tags = "用户相关接口")
@RestController
@RequestMapping("api/user")
public class UserInfoApiController {

    @Resource
    private UserInfoService userInfoService;

    // 用户手机号登录
    @ApiOperation(value = "用户手机号登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo) {
        Map<String, Object> loginInfoMap = userInfoService.loginUser(loginVo);
        return Result.ok(loginInfoMap);
    }

    // 用户认证接口
    @ApiOperation(value = "用户认证接口")
    @PostMapping("/auth/userAuth")
    public Result<Boolean> userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        // 传递两个参数，第一个参数用户id，第二个参数认证数据的vo对象
        userInfoService.userAuth(AuthContextHolder.getUserId(request), userAuthVo);
        return Result.ok();
    }

    // 获取用户id信息接口
    @ApiOperation(value = "获取用户id信息接口")
    @GetMapping("auth/getUserInfo")
    public Result<UserInfo> getUserInfo(HttpServletRequest request) {
        String userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }
}
